﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Objects;

namespace Fujitsu.JiraAnalysis.DAL
{
    public interface IPriorityStorage
    {
        List<PriorityMapping> GetIssuePriorityMappingForProject(int projectId);
    }

    public class PriorityStorage : IPriorityStorage
    {
        public List<PriorityMapping> GetIssuePriorityMappingForProject(int projectId)
        {
            using(var db = new JiraAnalysisDBEntities())
            {
                return (from p in db.PriorityMapping
                        where p.ProjectID == projectId
                        select p).ToList();
            }
        }
        public List<Priority> GetPriorities()
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.Priority.ToList();
            }
        }

        public void UpdateMapping(int projectId, int jiraPriorityId, int dbPriorityId, string jiraPriorityName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var priorityMapping = (from priority in db.PriorityMapping
                                     where priority.ProjectID == projectId
                                     && priority.JiraPriorityID == jiraPriorityId
                                     && priority.Name == jiraPriorityName
                                     select priority).FirstOrDefault();

                var isToAddPriorityMapping = false;

                if (priorityMapping != null)
                {
                    if (priorityMapping.PriorityID != dbPriorityId)
                    {
                        db.PriorityMapping.DeleteObject(priorityMapping);
                        isToAddPriorityMapping = true;
                    }
                }
                else
                {
                    isToAddPriorityMapping = true;
                }

                if (isToAddPriorityMapping)
                {
                    var priorityNewMapping = new PriorityMapping
                    {
                        ProjectID = projectId,
                        JiraPriorityID = jiraPriorityId,
                        PriorityID = dbPriorityId,
                        Name = jiraPriorityName
                    };
                    try
                    {
                        db.PriorityMapping.AddObject(priorityNewMapping);
                        db.SaveChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        db.Refresh(RefreshMode.ClientWins, priorityNewMapping);
                        db.SaveChanges();
                    }
                    catch (DataException)
                    {
                    }
                }
            }
        }

        public int GetPriorityMapping(int projectId, int jiraPriorityId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var priorityMapping = db.PriorityMapping.Where(r => r.ProjectID == projectId && r.JiraPriorityID == jiraPriorityId).FirstOrDefault();
                if (priorityMapping != null)
                    return priorityMapping.PriorityID;
                return -1;
            }
        }

        public void UpdateResolutionName(int projectId, int jiraPriorityId, string priorityName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var priorityMap = db.PriorityMapping.Where(
                        s => s.ProjectID == projectId && s.JiraPriorityID == jiraPriorityId).
                        FirstOrDefault();
                if (priorityMap == null)
                    return;
                priorityMap.Name = priorityName;
                try
                {
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, priorityMap);
                    db.SaveChanges();
                }
                catch (DataException)
                {
                }
            }
        }

        public List<PriorityMapping> GetPriorityMappings(int projcetId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.PriorityMapping.Where(i => i.ProjectID == projcetId).ToList();
                try
                {
                    return db.PriorityMapping.Where(i => i.ProjectID == projcetId).ToList();
                }
                catch (EntityException ex)
                {
                    return new List<PriorityMapping>();
                }
            }
        }

        public void DeletePriorityMapping(int projectId, int jiraPriorityId, int priorityId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var dbPriorityMapping = db.PriorityMapping.FirstOrDefault(i => i.ProjectID == projectId && i.JiraPriorityID == jiraPriorityId && i.PriorityID == priorityId);
                if (dbPriorityMapping == null)
                    return;

                try
                {
                    db.PriorityMapping.DeleteObject(dbPriorityMapping);
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, dbPriorityMapping);
                    db.SaveChanges();
                }
                catch (DataException ex)
                {
                    //EventLogger.CreateInstance(eventLog).LogWriteError(string.Concat("Can't delete issue from DB.", Environment.NewLine, ex.StackTrace));
                }
            }
        }

        public void DeletePriorityMappingOfProject(int projcetId)
        {
            var priorityMappings = GetPriorityMappings(projcetId);
            foreach (var priorityMapping in priorityMappings)
            {
                DeletePriorityMapping(priorityMapping.ProjectID, priorityMapping.JiraPriorityID, priorityMapping.PriorityID);
            }
        }
    }
}
